﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class appleader_weight_directToIndi : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Session["currentprojectid"] != null)
            {
                int projectid = Convert.ToInt32(Session["currentprojectid"]);
                //Session["currentprojectid"] = null;
                int weightmissionid = MasApp.BLL.Search.SearchWeightMissionID(projectid);

                MasApp.Model.weight we = new MasApp.Model.weight();
                we.Weightmissionid = weightmissionid;
                we.Elementtype = "指标";

                foreach (GridViewRow gr in GridView1.Rows)
                {
                    Label lb_cur = (Label)gr.FindControl("lb_cur");
                    Label lb = (Label)gr.FindControl("Label1");
                    we.Elementid = Convert.ToInt32(lb.Text);
                    lb_cur.Text = MasApp.BLL.Search.SearhWeightValue(we).ToString("F4");
                }
            }
            else
            {
                MasApp.BLL.ResponseHelper.alert("非法操作");
            }
        }
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        int projectid = int.Parse(Session["currentprojectid"].ToString());
        //准备指标集
        //DataTable dt_indi = new DataTable();
        //dt_indi = app.BLL.Search.SearchIndicatorIDandName(projectid);

        //准备权重model
        MasApp.Model.weight we = new MasApp.Model.weight();
        we.Weightmissionid = MasApp.BLL.Search.SearchWeightMissionID(projectid);
        we.Elementtype = "指标";
        //得出所有的值相加的结果
        float allValue = 0.0F;
        foreach (GridViewRow gr in GridView1.Rows)
        {
            TextBox tb = (TextBox)gr.Cells[2].FindControl("TextBox1");
            allValue += Convert.ToSingle(tb.Text);
        }
        //逐个赋权
        foreach (GridViewRow gr in GridView1.Rows)
        {
            TextBox tb = (TextBox)gr.FindControl("TextBox1");
            Label lbId = (Label)gr.FindControl("Label1");

            we.Elementid = int.Parse(lbId.Text);
            we.Weightvalue = (Convert.ToSingle(tb.Text)) / (allValue);
            if (MasApp.DAL.Search.WeightID(we) > 0)
            {
                MasApp.BLL.Update.UpdateWeight(we);
            }
            else
            {
                MasApp.BLL.reg.RegWeight(we);
            }
        }
        MasApp.BLL.ResponseHelper.alert("修改成功", "directToIndi.aspx?ruleid=" + Request.QueryString[0].ToString());
    }
}
